﻿#include "tcpclient.h"

TCPClient::TCPClient()
{


}

void TCPClient::InitSocket(QString serverIP, int port)
{
	mTcpSocket = new QTcpSocket;//初始化Tcp连接套接字
	mServerAddress.setAddress(serverIP);// "10.0.0.177");//设置服务器地址

	ConnectToServer();

}

void TCPClient::InitTimer()
{
	mTimer = new QTimer;//初始化定时器对象
	connect(mTimer, &QTimer::timeout, this, [=]
		{
			//		SendData();
		});
	mTimer->start(1000);//每隔一秒发送一次数据
}

void TCPClient::ConnectToServer()
{
	quint16 _port = 10010;//设置服务器端口
	connect(mTcpSocket, &QTcpSocket::connected, this, [=] {
		qDebug() << "TCP ==> Connect To Server Successful!" << endl;
		});
	mTcpSocket->connectToHost(mServerAddress, _port, QIODevice::WriteOnly);//连接服务器

}

void TCPClient::SendData(QByteArray _data)
{
	//QByteArray _data = "hello";
	if (-1 != mTcpSocket->write(_data.data(), _data.length()))
	{
		qDebug() << "TCP ==> Send data : " << _data.toHex('-') << endl;
	}
	mTcpSocket->flush();
}
